package com.example.hellonotes.utils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.MediaColumns;
import android.widget.Toast;

/**
 *  处理图片相关的工具类
 * @author Administrator
 *
 */
public class ImageUtils {

	public static final int REQUEST_CODE_FROM_CAMERA = 5001;
	public static final int REQUEST_CODE_FROM_ALBUM = 5002;
	
	/**
	 * 存放拍照图片的uri地址
	 */
	public static Uri imageUriFromCamera;

	/**
	 * 显示获取照片不同方式对话框
	 */
	public static void showImagePickDialog(final Activity activity) {
		String title = "选择获取图片方式";
		String[] items = new String[]{"拍照", "相册"};
		new AlertDialog.Builder(activity)
			.setTitle(title)
			.setItems(items, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
					switch (which) {
					case 0:
						pickImageFromCamera(activity);
						break;
					case 1:
						pickImageFromAlbum(activity);
						break;
					default:
						break;
					}
				}
			})
			.show();
	}
	
	/**
	 * 打开相机拍照获取图片
	 */
	public static void pickImageFromCamera(final Activity activity) {
		imageUriFromCamera = createImageUri(activity);
		
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUriFromCamera);
		System.out.println("URL地址是:"+imageUriFromCamera);
		activity.startActivityForResult(intent, REQUEST_CODE_FROM_CAMERA);
		/*Intent vvideo = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		PhoneFile = new File(Environment.getExternalStorageDirectory()
				.getAbsoluteFile() + "/" + getImgTime() + ".jpg");
		System.out.println(PhoneFile);
		vvideo.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(PhoneFile));*/
		/*activity.startActivityForResult(intent, REQUEST_CODE_FROM_CAMERA);*/
	}

	/*	public static String getImgTime() {
		SimpleDateFormat DateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		Date curDate = new Date();
		String str = DateFormat.format(curDate);
		return str;
	}*/
	
	/**
	 * 打开本地相册选取图片获取图片的  uri！！！
	 * 以下两种方法获取的图片路径不同
	 * content://com.android.providers.media.documents/document/image%3A23
	 * content://media/external/images/media/23
	 * 不知道有啥区别  使用起来好像一样
	 */
/*	public static void pickImageFromAlbum(final Activity activity) {
		Intent intent = new Intent();
		intent.setAction(Intent.ACTION_GET_CONTENT);
		intent.setType("image/*");
		activity.startActivityForResult(intent, REQUEST_CODE_FROM_ALBUM);
	}*/
	
	public static void pickImageFromAlbum(final Activity activity) {
		
		Intent intent = new Intent( Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
		intent.setType("image/*");
		activity.startActivityForResult(intent, REQUEST_CODE_FROM_ALBUM);
	}
	
	/**
	 * 打开本地相册选取图片2
	 */
	public static void pickImageFromAlbum2(final Activity activity) {
		Intent intent = new Intent();
		intent.setAction(Intent.ACTION_PICK);
		intent.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
		activity.startActivityForResult(intent, REQUEST_CODE_FROM_ALBUM);
	}
	
	/**
	 * 将图片保存到SD中
	 */
	public static void saveFile(Context context, Bitmap bm, String fileName) throws IOException {
		// 未安装SD卡时不做保存
		String storageState = Environment.getExternalStorageState();
		if(!storageState.equals(Environment.MEDIA_MOUNTED)) {
			ToastUtils.showToast(context, "未检测到SD卡", Toast.LENGTH_SHORT);
			return;
		}
		
		// 图片文件保存路径
		File storageDirectory = Environment.getExternalStorageDirectory();
		File path = new File(storageDirectory, "/boreweibo/weiboimg");
		// 图片路径不存在创建之
		if (!path.exists()) {
			path.mkdirs();
		}
		// 图片文件如果不存在创建之
		File myCaptureFile = new File(path, fileName);
		if (!myCaptureFile.exists()) {
			myCaptureFile.createNewFile();
		}
		// 将图片压缩至文件对应的流里,即保存图片至该文件中
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
		bm.compress(Bitmap.CompressFormat.JPEG, 100, bos);
		bos.flush();
		bos.close();
	}
	
	/**
	 * 创建一条图片uri,用于保存拍照后的照片
	 */
	private static Uri createImageUri(Context context) {
		String name = "boreWbImg" + System.currentTimeMillis();
		ContentValues values = new ContentValues();
		values.put(MediaColumns.TITLE, name);
		values.put(MediaColumns.DISPLAY_NAME, name + ".jpeg");
		values.put(MediaColumns.MIME_TYPE, "image/jpeg");
		Uri uri = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
		return uri;
	}
	
	/**
	 * 删除一条图片
	 */
	public static void deleteImageUri(Context context, Uri uri) {
		context.getContentResolver().delete(imageUriFromCamera, null, null);
	}

	/**
	 * 获取图片文件路径
	 */
	public static String getImageAbsolutePath(Context context, Uri uri) {
		Cursor cursor = MediaStore.Images.Media.query(context.getContentResolver(), uri, 
				new String[]{MediaColumns.DATA});
		if(cursor.moveToFirst()) {
			return cursor.getString(0);
		}
		return null;
	}
}
